package bb.lanxing.mvp.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import bb.lanxing.model.data.ResponseData;
import bb.lanxing.mvp.model.i.ISetupMapStyleModel;
//import bb.lanxing.network.BiciHttpClient;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.util.Log;
import okhttp3.Call;
import okhttp3.Response;
import rx.Observable;
import rx.schedulers.Schedulers;

public class SetupMapStyleModel implements ISetupMapStyleModel {
    final private static String TAG = "SetupMapStyleModel";

    @Override
    public Observable<ResponseData> uploadWorkoutMapId(long j, int i) {
        final Call uploadWorkoutMapId = MyHttpClient.uploadWorkoutMapId(j, i);
        return Observable.<String>create(subscriber -> {
            try {
                Response execute = uploadWorkoutMapId.execute();
                if (execute.isSuccessful()) {
                    String string = execute.body().string();
                    Log.d(TAG, " response : " + execute + " body : " + string);
                    if (!subscriber.isUnsubscribed() || !uploadWorkoutMapId.isCanceled()) {
                        subscriber.onNext(string);
                    }
                } else if (execute.code() == 403) {
                    execute.body().string();
                } else if (execute.code() == 400) {
                    execute.body().string();
                }
                subscriber.onCompleted();
            } catch (IOException | IllegalStateException e) {
                subscriber.onError(e);
            }
        }).subscribeOn(Schedulers.io()).flatMap(str -> {
            ResponseData responseData = null;
            JSONObject parseObject = null;
            try {
                parseObject = JSON.parseObject(str);
                responseData = new ResponseData();
            } catch (Exception unused) {
            }
            try {
                if (null != parseObject && null != responseData) {
                    responseData.setCode(parseObject.getInteger("code"));
                    responseData.setMsg(parseObject.getString("msg"));
                }
            } catch (Exception unused2) {
                return Observable.just(responseData);
            }
            return Observable.just(responseData);
        });
    }
}